package com.iwhalecloud.bss.kite.cucc.service.inf;

import cn.chinaunicom.open.common.exceptions.compose.COMPCommunicationException;
import cn.chinaunicom.open.common.exceptions.compose.COMPInvokeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfUnifyService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttachmentDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.order.FileVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.order.PropertyValueVO;
import com.iwhalecloud.bss.kite.cucc.common.callclient.constant.CallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalInfConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.LocalTokenUtil;
import com.iwhalecloud.bss.unify.dto.req.FileToStringUploadV1V2Req;
import com.iwhalecloud.bss.unify.dto.req.PropertyValueGetV1V1Req;
import com.iwhalecloud.bss.unify.dto.req.PropertyValueListV1V3Req;
import com.iwhalecloud.bss.unify.dto.resp.FileToStringUploadV1V2Rsp;
import com.iwhalecloud.bss.unify.dto.resp.PropertyValueGetV1V1Rsp;
import com.iwhalecloud.bss.unify.dto.resp.PropertyValueListV1V3Rsp;
import com.iwhalecloud.bss.unify.impl.CoCenterServiceImpl;
import com.iwhalecloud.bss.unify.impl.UploadFileServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
@Service
public class InfUnifyService implements IInfUnifyService {
    @Resource
    UploadFileServiceImpl uploadFileService;
    @Resource
    CoCenterServiceImpl CoCenterService;
    @Override
    public FileVO marketCenterFileUpload(Map<String, String> reqMap, byte[] bytes) {
        FileToStringUploadV1V2Req fileToStringUploadV1V2Req = new FileToStringUploadV1V2Req();
        Base64.Encoder encoder = Base64.getEncoder();
        String body = encoder.encodeToString(bytes);
        fileToStringUploadV1V2Req.setFileBody(body);
        fileToStringUploadV1V2Req.setFileName(KiteMapUtils.getString(reqMap,"FILE_NAME"));
        fileToStringUploadV1V2Req.setInfoType(KiteMapUtils.getString(reqMap,"INFO_TYPE"));
        fileToStringUploadV1V2Req.setSourceRequest(LocalInfConsts.APP_ID);
        fileToStringUploadV1V2Req.setTransId(LocalTokenUtil.getTransId());
        FileVO fileVO = new FileVO();
        try {
            FileToStringUploadV1V2Rsp fileToStringUploadV1V2RspEntity = uploadFileService.invokeInf(fileToStringUploadV1V2Req);
            if("0000".equals(fileToStringUploadV1V2RspEntity.getCode())){
                List<FileToStringUploadV1V2Rsp.Data> datas = fileToStringUploadV1V2RspEntity.getData();
                if(KiteListUtils.isNotEmpty(datas)){
                    FileToStringUploadV1V2Rsp.Data  data = datas.get(0);
                    fileVO.setFileId(data.getLocation());
                    fileVO.setAccessoryId(data.getFlowId());
                }
            }else{
                ErrorConsts.INF_INVOKE_ERROR.throwOut("json_chinaUnicom_govEnterprise_marketingCenter_fileToStringUpload", "营销中心合同上传接口调用失败",
                    "营销中心合同上传接口调用失败" + fileToStringUploadV1V2RspEntity.getMessage());
                ErrorConsts.INF_INVOKE_EXCEPTION.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(),fileToStringUploadV1V2RspEntity.getMessage());
            }

        } catch (COMPInvokeException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "营销中心合同上传接口调用失败",
                "营销中心合同上传接口调用失败" + e.getMessage());
        } catch (COMPCommunicationException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "营销中心合同上传接口调用失败",
                "营销中心合同上传接口调用失败" + e.getMessage());
        }
        return fileVO;
    }
    @Override
    public void insertValueList(Map<String, Object> reqMap) {
        PropertyValueListV1V3Req propertyValueListV1V3Req = new PropertyValueListV1V3Req();
        List<PropertyValueListV1V3Req.Valueinfolist> list=new ArrayList();
        PropertyValueListV1V3Req.Valueinfolist valueinfolist= new PropertyValueListV1V3Req.Valueinfolist();
        valueinfolist.setProperty3(KiteMapUtils.getString(reqMap,"property3"));
        valueinfolist.setProperty2(KiteMapUtils.getString(reqMap,"property2"));
        valueinfolist.setProperty1(KiteMapUtils.getString(reqMap,"property1"));
        list.add(valueinfolist);
        propertyValueListV1V3Req.setValueinfolist(list);
        propertyValueListV1V3Req.setProvincename(KiteMapUtils.getString(reqMap,"provinceName"));
        propertyValueListV1V3Req.setCommoditycode(KiteMapUtils.getString(reqMap,"commodityCode"));
        propertyValueListV1V3Req.setOperationbusitype("VPN");
        propertyValueListV1V3Req.setSkucode(KiteMapUtils.getString(reqMap,"skuCode"));
        try {
          String code=CoCenterService.saveProperty(propertyValueListV1V3Req);
            Map<String, Object> resp = JSON.parseObject(code, Map.class);
            JSONObject uniBssHead = KiteMapUtils.getValue(resp, "UNI_BSS_HEAD");
            if (KiteStringUtils.equals(KiteMapUtils.getString(uniBssHead, "RESP_CODE"), LocalInfConsts.RESP_SUCCESS)) {
                Map<String, Object> UNI_BSS_BODY = (Map<String, Object>) resp.get("UNI_BSS_BODY");
                Map<String, Object> PROPERTY_VALUE_LIST_RSP = (Map<String, Object>) UNI_BSS_BODY.get("PROPERTY_VALUE_LIST_RSP");

                Integer  codes = KiteMapUtils.getInteger(PROPERTY_VALUE_LIST_RSP,"code");
                if(codes.intValue()==200){
                }else{
                    ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品新增接口调用失败",
                        "产商品新增接口调用失败" );
                }
            }else{
                ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品新增接口调用失败",
                    "产商品新增接口调用失败" );
            }
            } catch (COMPInvokeException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品新增接口调用失败",
                "产商品新增接口调用失败" + e.getMessage());
        } catch (COMPCommunicationException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品新增接口调用失败",
                "产商品新增接口调用失败" + e.getMessage());
        }
    }
    @Override
    public  List<PropertyValueVO> queryValueList(Map<String, Object> reqMap) {
        PropertyValueGetV1V1Req PropertyValueGetV1V1Req = new PropertyValueGetV1V1Req();
        PropertyValueGetV1V1Req.setSkucode(KiteMapUtils.getString(reqMap,"skuCode"));
        PropertyValueGetV1V1Req.setCommoditycode(KiteMapUtils.getString(reqMap,"commodityCode"));
        PropertyValueGetV1V1Req.setOperationbusitype("VPN");
        PropertyValueGetV1V1Req.setProvincename(KiteMapUtils.getString(reqMap,"provinceName"));
        PropertyValueGetV1V1Req.setProperty2(KiteMapUtils.getString(reqMap,"property3"));
        PropertyValueGetV1V1Req.setProperty1(KiteMapUtils.getString(reqMap,"property1"));
        PropertyValueGetV1V1Req.setProperty3(KiteMapUtils.getString(reqMap,"property2"));
        List<PropertyValueVO> lists=new ArrayList<>();
        try {
            PropertyValueGetV1V1Rsp PropertyValueListV1V3Rsp = CoCenterService.getProperty(PropertyValueGetV1V1Req);
            if(PropertyValueListV1V3Rsp.getCode().intValue()==200){
                PropertyValueGetV1V1Rsp.Data Rspdata = PropertyValueListV1V3Rsp.getData();
                if(KiteListUtils.isNotEmpty(Rspdata.getValueinfolist())){
                    String Valueinfolist = JSONObject.toJSONString(Rspdata.getValueinfolist());
                    lists=JSONObject.parseArray(Valueinfolist, PropertyValueVO.class);
                }

            }else{
                ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品查询接口调用失败",
                    "产商品查询接口调用失败");
            }

        } catch (COMPInvokeException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品查询接口调用失败",
                "产商品查询接口调用失败" + e.getMessage());
        } catch (COMPCommunicationException e) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.CREATE_UPLOADCONTRACT.getServiceCode(), "产商品查询接口调用失败",
                "产商品查询接口调用失败" + e.getMessage());
        }
        return lists;
    }
}
